//
//  KDLoadFrame.m
//  KDTools
//
//  Created by 斌 on 15/11/19.
//  Copyright © 2015年 斌. All rights reserved.
//

#import "KDLoadFrame.h"

@implementation KDLoadFrame{

    UIImageView *imgLoadBg;
    UILabel *txtLoad;
    UIActivityIndicatorView *activity;

}

- (id)init{
    
    self = [super init];
    if (self) {
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(operation:) name:@"KDLoadFrame" object:nil];
        
        self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.3];
        self.frame = [[UIScreen mainScreen] bounds];
        CGFloat screenWidth = [[UIScreen mainScreen] bounds].size.width;
        CGFloat screenHeight = [[UIScreen mainScreen] bounds].size.height;
        
        imgLoadBg = [[UIImageView alloc]initWithFrame:CGRectMake(screenWidth/2 -43, screenHeight/2 -43, 86, 86)];
        imgLoadBg.image = [self roundCorners:[UIColor colorWithWhite:0 alpha:0.5]];
        [self addSubview:imgLoadBg];
        
        activity = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 35, 35)];
        [activity setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhiteLarge];
        [activity setCenter:CGPointMake(imgLoadBg.frame.size.width/2,imgLoadBg.frame.size.height/2-10)];
        [activity startAnimating];
        [imgLoadBg addSubview:activity];
        
        txtLoad = [[UILabel alloc]initWithFrame:CGRectMake(0, 86 -25, 86, 20)];
        txtLoad.text = @"请 稍 等...";
        txtLoad.font = [UIFont systemFontOfSize:14];
        txtLoad.textAlignment = NSTextAlignmentCenter;
        txtLoad.backgroundColor = [UIColor clearColor];
        txtLoad.textColor = [UIColor whiteColor];
        [imgLoadBg addSubview:txtLoad];
        self.alpha = 0;
    }
    return self;
}

- (void)operation:(NSNotification *)notification{
    
    [self reloadView];
    
    if ([[notification object]isEqual:@"show"]) {
        [txtLoad setFrame:CGRectMake(0, 86 -25, 86, 20)];
    }else if ([[notification object]isEqual:@"error"]) {
        [txtLoad setFrame:CGRectMake(0,(86-20)/2,86,20)];
    }else{
        
        if (![txtLoad.text hasSuffix:@"."]){
            [txtLoad setFrame:CGRectMake(0,(86-20)/2,86,20)];
        }else{
            [txtLoad setFrame:CGRectMake(0, 86 -25, 86, 20)];
        }
    }
    
    
    
    [UIView animateWithDuration:0.3 animations:^(void){
        if ([[notification object]isEqual:@"show"]) {
            [txtLoad setAlpha:1];
            [activity setAlpha:1];
            
            txtLoad.text = @"请 稍 等...";
            self.alpha = 1;
            
        }else if ([[notification object]isEqual:@"hide"]) {
            
            self.alpha = 0;
            
            
        }else if ([[notification object]isEqual:@"error"]) {
            
            [txtLoad setAlpha:1];
            [activity setAlpha:0];
            
            txtLoad.text = @"网络不可用";
            self.alpha = 1;
            [self performSelector:@selector(hideErr) withObject:nil afterDelay:2];
            
        }else{
            
            [txtLoad setAlpha:1];
            [activity setAlpha:1];
            txtLoad.text=[notification object];
            self.alpha = 1;
            
            if (![txtLoad.text hasSuffix:@"."]){
                [activity setAlpha:0];
                [self performSelector:@selector(hideErr) withObject:nil afterDelay:2];
            }
            
        }
    }];
}

- (void)hideErr{
    [UIView animateWithDuration:0.3 animations:^(void){
        self.alpha = 0;
        
    }];
}


- (void)reloadView{
    self.frame =  [[UIScreen mainScreen] bounds];
    CGFloat screenWidth = [[UIScreen mainScreen] bounds].size.width;
    CGFloat screenHeight = [[UIScreen mainScreen] bounds].size.height;
    imgLoadBg.frame = CGRectMake(screenWidth/2 -43, screenHeight/2 -43, 86, 86);
}


+ (void)withState:(KDLoadFrameState)state{

    NSString *tmpString ;
    switch (state) {
        case 0:
            tmpString = @"show";
            break;
        case 1:
            tmpString = @"hide";
            break;
        case 2:
            tmpString = @"error";
            break;
        default:
            return;
    }
    
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"KDLoadFrame" object:tmpString];
}


+ (void)withOtherStateString:(NSString *)String{
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"KDLoadFrame" object:String];
}


- (UIImage *) roundCorners : (UIColor*)color
{
    UIImage *image = [UIImage new];
    
    CGFloat width = 86;
    CGFloat height = 86;
    UIGraphicsBeginImageContext(CGSizeMake(width, height));

    CGFloat radius = (width + height) * 0.05;
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextMoveToPoint(context, radius, 0);
    
    CGContextAddLineToPoint(context, width - radius, 0);
    CGContextAddArc(context, width - radius, radius, radius, -0.5 * M_PI, 0.0, 0);
    
    CGContextAddLineToPoint(context, width, height - radius);
    CGContextAddArc(context, width - radius, height - radius, radius, 0.0, 0.5 * M_PI, 0);
    
    CGContextAddLineToPoint(context, radius, height);
    CGContextAddArc(context, radius, height - radius, radius, 0.5 * M_PI, M_PI, 0);
    
    CGContextAddLineToPoint(context, 0, radius);
    CGContextAddArc(context, radius, radius, radius, M_PI, 1.5 * M_PI, 0);
    
    CGContextClosePath(context);
    
    CGContextSetFillColorWithColor(context, color.CGColor);
    CGContextDrawPath(context, kCGPathFill);
    
    image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image;
}
@end
